import Vue from 'vue'
import Vuex from 'vuex'
import $mConfig from '@/config/index.config.js'
import $mRoutesConfig from '@/config/routes.config.js'
import $mRouter from '@/common/router.js'
import * as $apis from '@/apis/index.js'
import $mUtils from '@/common/utils.js'


// $apis.getuserclientip().then((res)=>{
//     console.log(res)
// })

Vue.use(Vuex)
// const TOKEN = process.env.NODE_ENV == "development" ? 'aY9K7' : uni.getStorageSync("token") || "";
const TOKEN = uni.getStorageSync("token") || "";
// const TOKEN = $mUtils.getCookie('token') || "";

// console.log($mUtils.getCookie('token'))
const USER_INFO = uni.getStorageSync("userInfo") || {};
const cCity = uni.getStorageSync("cCity") || {};
const initConfig = uni.getStorageSync("initConfig") || {};
const sessionId = uni.getStorageSync("sessionId") || '';
const scene = uni.getStorageSync("scene") || {};
const store = new Vuex.Store({
    state: {
        // 前端token
        vuex_token: TOKEN,
        // 用户信息 头像 昵称
        vuex_userInfo: USER_INFO,
        // 全局验证码时间
        vuex_codeTime: 60,
		// 邀请码
		vuex_ucode: '',
        vuex_cCity: cCity,
        vuex_testId: [295,136,1,2,3],
		vuex_scopes: $mConfig.scopes,
		vuex_sessionId: sessionId,
		// 分享参数
		vuex_scene: scene,
		// 初始化配置
		vuex_initConfig: {
			home_page: {
				home_order_type1: 'https://oss.buwangyun.com/upload/20210427/4efc2d9afaaf2f4e3af85ed2e228fc22.png',
				home_order_type2: 'https://oss.buwangyun.com/upload/20210427/a9d6c234fbca67652423eb4b1a75ddfd.png',
				home_small_banner: 'https://oss.buwangyun.com/upload/20210427/3cfe1cb86f8178496d2e155b72770065.png'
			},
			order_page: {
				order_shop_cart: 'https://oss.buwangyun.com/upload/20210428/87631010dc99f4768939332acd4dc4ac.png'
			},
			confirm_order: {
				confirm_shop_icon: 'https://oss.buwangyun.com/upload/20210430/f0a4a115e5d4ea103b8d18cb7ddcc9ad.png',
				confirm_order_icon: 'https://oss.buwangyun.com/upload/20210430/90c770cfa22fcd7d3cf692a6cca462d6.png',
				confirm_money_icon: 'https://oss.buwangyun.com/upload/20210430/5a8336269b7cb7c85c0ec14ebe1a2e91.png',
				confirm_address_icon: 'https://oss.buwangyun.com/upload/20210430/f2ecc4f021be5599e34849f8f4ba44e9.png'
			},
			shop_page: {
				shop_title_icon1: 'https://oss.buwangyun.com/upload/20210430/6241fd4bdf964fb733cafec490031aec.png',
				shop_title_icon2: 'https://oss.buwangyun.com/upload/20210430/ca41b0f53ede4b3ddeab5d331d3ebb79.png',
				shop_title_icon3: 'https://oss.buwangyun.com/upload/20210430/6417d76049276d5dd45c29094454fef1.png',
				shop_title_icon4: 'https://oss.buwangyun.com/upload/20210430/75264dc81273ea08223f4cbccd5159ed.png'
			},
			common_page: {
				logo_img: 'https://oss.buwangyun.com/upload/20210416/2fd858260c0e0003237004797ca3ade0.png'
			}
		}
    },
    getters: {
        // 用户是否登录
        hasLogin: state => {
            if (state.token) {
                return true;
            } else {
                return false
            }
        },
    },
    mutations: {
		SET_SESSIONID(state,sessionId){
			Vue.set(state, 'vuex_sessionId', sessionId);
			uni.setStorageSync("sessionId", sessionId);
		},
		SET_INITCONFIG(state,initConfig){
			Vue.set(state, 'vuex_initConfig', initConfig);
		},
		SET_UCODE(state,ucode){
			Vue.set(state, 'vuex_ucode', ucode);
		},
        SET_TOKEN(state, token) {
            state.vuex_token = token;
            uni.setStorageSync("token", token);
        },
        SET_USERINFO(state, res) {
            state.vuex_userInfo = res;
            uni.setStorageSync("userInfo", res);
        },
        SET_LOGOUT(state) { //退出登录
            state.vuex_token = "";
            uni.setStorageSync("token", "");
            state.vuex_userInfo = {};
            uni.setStorageSync("userInfo", {});
        },
        CODE_TIME(state) {
            state.codeTime--
        },
		SET_SCENE(state, scene){
			Vue.set(state, 'vuex_scene', scene);
			uni.setStorageSync("scene", scene);
		}
    },
    actions: {
        //获取版本信息
        getEdition({
            commit
        }, cd) {
            $apis.postIndexEdition({}, (res) => {
                commit('SET_EDITION', res);
                if (typeof cd != 'undefined' && cd instanceof Function) {
                    cd();
                }
            })
        },
        // 登录过期 重新登录
        reLogin({
            commit
        }, info) {
            commit("SET_LOGOUT", "");
			$mRouter.push({
			    route: $mRoutesConfig['loginIndex']
			})
        },
		// 登录
		Login({
			commit
		}, info){
			const {token,session_id} = info;
			commit('SET_TOKEN',token);
			commit('SET_SESSIONID',session_id);
			this.dispatch('getUser');
		},
        getUser({ // 更新用户信息
            commit,
            state
        }, cd) {
            $apis.getUserInfo({isAlertLoad: true}, (res) => {
				commit('SET_USERINFO', res);
                if (typeof cd != 'undefined' && cd instanceof Function) {
                    cd(res);
                }
            })
        },
        // 发送短信验证码倒计时
        codeTime({ // 验证码倒计时
            commit
        }) {
            setTimeout(() => {
                commit('CODE_TIME')
                if (this.state.codeTime == 0) {
                    this.state.codeTime = 60
                    return
                } else {
                    this.dispatch('codeTime')
                }
            }, 1000)
        }
    }
})

export default store
